package com.project2.stuttewebapp.server;

import com.project2.stuttewebapp.client.GreetingService;
import com.project2.stuttewebapp.shared.FieldVerifier;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
	
	/**
	 * The server side functionality to check for stutter words.
	 * @param input The textarea input
     * 
     * @throws IllegalArgumentException Signals that an Argument exception has occurred.
	 */
	public String greetServer(String input) throws IllegalArgumentException {
		// Verify that the input is valid. 
		if (!FieldVerifier.isValidName(input)) {
			// If the input is not valid, throw an IllegalArgumentException back to
			// the client.
			throw new IllegalArgumentException(
					"The text must be at least 10 characters long");
		}

		//Findstutter functionality for the given input.
		String outputToClient = findStutter(input);
		//If there are no repeated words, show to client the message
		if(outputToClient.isEmpty())
			outputToClient = "No repeated words found.";
					
		return outputToClient;

	}

	/**
	 * Find stuttered words in a given input
	 * @author Poornima
	 * @param  textInput -  The text entered in the webpage
	 * @return  String - stuttered words
	 *
	 */

	private String findStutter(String textInput){
		String word1 = null;
		String word2 = null;
		String[] line = null;
		String[] tokens = null;
		StringBuffer buf = new StringBuffer();
		int j= 0;
		int lineCount=0;
		line = textInput.split("\\\n");
		//Repeat the process for each line in the input
		while(j < line.length){
			lineCount++;
			//To remove the alpha-numeric characters in the input
			line[j] = line[j].replaceAll ("[^a-zA-Z0-9 ]", "");
			//To change the input to lower case so as to check for stuttered words with case insensitiveness
			line[j] = line[j].toLowerCase();
			//Split the line to tokens based on the delimit characters like \n,.'&{}+-;:
			tokens= splitBasedOnDelimiter(line[j].toLowerCase());
			
			//For the splitted tokens in each each line, we check for repeated words
			for(int i=0; i<tokens.length; i++){
				word1 = tokens[i];
				if(word1.equals(word2)){
					//If repeated, add it to a string buffer to send to output
					buf.append("Repeated word on line "+ lineCount+ " : "+ word1 + " " + word2 +"<br>");
				}
				else
					word2 = word1;
			}//end-of-for
			word2 = tokens[tokens.length-1];
			j++;
		}// end-of-while 	

		return buf.toString();
	}   
	/**
	 * Check the Delimiters in each line	
	 * @param  line -  pass each line to split into tokens based on the delimiters
	 * @return  String[] - tokens 
	 *
	 */

	private String[] splitBasedOnDelimiter(String line){
		String[] tokens = null;
		String delimiter = "[\\n ,.\\\\!-+=;:?&{}]+";

		tokens = line.split(delimiter);
		return tokens;
	}	
}
